package com.hongning.railway.ui.main.fragments.college.exam;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.PagerSnapHelper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentCheckanswerBinding;
import com.hongning.railway.ui.adapter.AnswerCheckAdapter;

import java.util.Calendar;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;

/**
 * Author: zhangjing
 * DATA:2019-08-21 16:35
 * DESC:查看答案
 * email:zhangjingworkmol@163.com
 */
public class CheckAnswerFragment extends BaseFragment<FragmentCheckanswerBinding, CheckAnswerFragmentViewModel> {
    String examId;
    String resultId;
    int  type;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_checkanswer;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        examId = getArguments().getString(Constant.EXTRA_DATA);
        resultId = getArguments().getString(Constant.EXTRA_STRING);
        type = getArguments().getInt(Constant.EXTRA_INT);
    }
    @Override
    public CheckAnswerFragmentViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(CheckAnswerFragmentViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        binding.includeTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.includeTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        AnswerCheckAdapter answerAdapter = new AnswerCheckAdapter();
        answerAdapter.setViewModel(viewModel);
        binding.rvContent.setAdapter(answerAdapter);
        binding.rvContent.setOnTouchListener(new View.OnTouchListener() {
            private static final int MAX_CLICK_DURATION = 200;
            private long startClickTime;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        startClickTime = Calendar.getInstance().getTimeInMillis();
                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                        if(clickDuration < MAX_CLICK_DURATION) {
                            return false;
                        }
                    }
                }
                return true;
            }
        });
        if (type==1){
            binding.includeTitle.tvTopTitle.setText("专项答题");
        }else {
            binding.includeTitle.tvTopTitle.setText("每周答题");
        }
        PagerSnapHelper pagerSnapHelper=new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(binding.rvContent);
        viewModel.loadAnswerResult(examId,resultId);
    }
}
